var EST_PATH_PREFIX = "";

function setPathPrefix(path){
	EST_PATH_PREFIX =  path;
}

$(function() {
	/*$('[type=checkbox]').iCheck({
		checkboxClass : 'icheckbox_square-green',
	});*/
	setPathPrefix(disclosurePath);
	initData();
	//bindChartInvestClick();
	
	/*WASP_WIDGET.triggerIRadio("input[type='radio'][name='frequency']");
	WASP_WIDGET.triggerIRadio("input[type='radio'][name='titleSelector']",function(){
		bindChartInvestClick();
	});*/
	bindChartInvestClick();
});

function initData(){
	drwChartInvestData('1,2,3,4,5,6');
}

function bindChartInvestClick(){
	
	/*var frequency=$('#titleSelector').find('input[name=frequency]:checked').val();
	drwChartInvestData(frequency);
	*/
	$("div[id='titleSelector']").find("span.subSelector").each(function(index){
		$(this).on('click',function(){
			$('#titleSelector .font-color-white').each(function(index,element){
				$(element).removeClass('font-color-white');
			});
			$(this).toggleClass("font-color-white");
			var reportFrequency = $(this).attr("reportFrequency");
			drwChartInvestData(reportFrequency);
		});
	});
	/*$('#estimateDetail').find('input[type=checkbox]').each(function(index,obj){
		$(obj).on('ifChecked', function(event){
			var reportFrequency = "";
			$('#estimateDetail').find('input[type=checkbox]:checked').each(function(){
				reportFrequency += $(this).val() + ",";
			});
			drwChartInvestData(reportFrequency);
		}).on('ifUnchecked', function(event){
			var reportFrequency = "";
			$('#estimateDetail').find('input[type=checkbox]:checked').each(function(){
				if($(this).val() != null || $(this).val() != ''){
					reportFrequency += $(this).val() + ",";
				}
			});
			drwChartInvestData(reportFrequency);
		});
	});*/
}

function drwChartInvestData(reportFrequency){
	if(reportFrequency == '' || reportFrequency == null){
		reportFrequency = '1,2,3,4,5,6';
	}
	
	var b = getDateRange(reportFrequency);
	$.ajax({
		type:"GET",
		url: EST_PATH_PREFIX + "/countReportData.do?r="+Math.random(),
		data:{"reprotDateBgn" : b.startDate,
			  "reprotDateEnd" : b.endDate},
		dataType:"json",
		success:function(data){
			//估值材料未上传
			var unuploadedData = "";
			//估值材料已上传
			var uploadedData = "";
			//估值文件未上传
			var unuploadedFile = "";
			//估值文件已上传
			var uploadedFile = "";
			
			//拼装数据
			unuploadedData = data[0].UNUPLOADED_DATA;
			uploadedData = data[0].UPLOADED_DATA;
			unuploadedFile = data[0].UNUPLOADED_FILE;
			uploadedFile = data[0].UPLOADED_FILE;
			drwChart(unuploadedData,uploadedData,unuploadedFile,uploadedFile,reportFrequency);
		}
	});
}

function drwChart(unuploadedData,uploadedData,unuploadedFile,uploadedFile,reportFrequency){
	//饼形图表  
	var barChart = echarts.init(document.getElementById('estimateBarChart'));
	barChart.clear();  
	barChart.showLoading({text: '正在努力的读取数据中...'}); 
	
/*	option1 = {
		    title: {
		        left: 'left',
		        text: '估值材料概览:'
		    },
		    tooltip : {
		        trigger: 'item',
		        formatter: "{a} <br/>{b} : {c} ({d}%)"
		    },
		    series : [
		        {
		            type: 'pie',
		            radius : '90%',
		            center: ['50%', '50%'],
		            selectedMode: 'single',
		            data:[
		                {value:uploadedData, name: '已上传'},
		                {value:unuploadedData, name: '未上传'}
		            ],
		            color: ['#ff6633','#55aa77']
		        }
		    ]
		};*/
	var option1 = {
			grid: {
				top: '10%',
				
			},
			color: ["#87cefa","#DADADA"],
			title : {
				text: '估值材料',
				left:'center'
	        },
	        tooltip: {
	            trigger: 'item',
	            formatter: "{a} <br/>{b}: {c} ({d}%)"
	        },
	        legend: {
	            type: 'scroll',
	            orient: 'vertical',
	            right: '10',
	            bottom: '0',
	            data: ['已上传(含无需上传)','未上传']
	        },
		    series: [
		    	{
		    		name:'访问来源',
		            type:'pie',
		            radius: ['35%', '55%'],
		            center:['50%','50%'],
		            avoidLabelOverlap: false,
		            label: {
		                normal: {
		                    show: true,
		                    /*position: 'center'*/
		                },
		                emphasis: {
		                    show: true,
		                    textStyle: {
		                       // fontSize: '16',
		                        fontWeight: 'bold'
		                    }
		                }
		            },
		            labelLine: {
		                normal: {
		                    show: true
		                }
		            },
		            itemStyle:{
				    	  normal:{
				    		  label:{
				    			  show: true,
				    			  formatter: '{b} : {c} '
				    		  		},
				    		  labelLine :{show:true}
				    	  		}
				    		  },
		            data: [ 
			                {value:uploadedData, name: '已上传(含无需上传)'},
			                {value:unuploadedData, name: '未上传'}
			              ]
		        }
		   ]
		};
	
	//饼形图表  
	var barChart2 = echarts.init(document.getElementById('estimateBarChart2'));
	barChart2.clear();  
	barChart2.showLoading({text: '正在努力的读取数据中...'}); 
	/*option2 = {
		    title: {
		        left: 'left',
		        text: '估值文件概览:'
		    },
		    tooltip : {
		        trigger: 'item',
		        formatter: "{a} <br/>{b} : {c} ({d}%)"
		    },
		    series : [
		        {
		            type: 'pie',
		            radius : '90%',
		            center: ['50%', '50%'],
		            selectedMode: 'single',
		            data:[
		                {value:uploadedFile, name: '已上传'},
		                {value:unuploadedFile, name: '未上传'}
		            ],
		            color: ['#ff6633','#55aa77']
		        }
		    ]
		};*/
	
	var option2 = {
			color: ["#87cefa","#DADADA"],
			title : {
				text: '估值文件',
				left:'center'
	        },
	        tooltip: {
	            trigger: 'item',
	            formatter: "{a} <br/>{b}: {c} ({d}%)"
	        },
	        legend: {
	            type: 'scroll',
	            orient: 'vertical',
	            right: '30',
	            bottom: '0',
	            data: ['已上传','未上传']
	        },
		    series: [
		    	{
		    		name:'访问来源',
		            type:'pie',
		            radius: ['35%', '55%'],
		            center:['50%','50%'],
		            avoidLabelOverlap: false,
		            label: {
		                normal: {
		                    show: true,
		                    /*position: 'center'*/
		                },
		                emphasis: {
		                    show: true,
		                    textStyle: {
		                      //  fontSize: '16',
		                        fontWeight: 'bold'
		                    }
		                }
		            },
		            labelLine: {
		                normal: {
		                    show: true
		                }
		            },
		            itemStyle:{
				    	  normal:{
				    		  label:{
				    			  show: true,
				    			  formatter: '{b} : {c} '
				    		  		},
				    		  labelLine :{show:true}
				    	  		}
				    		  },
		            data: [ 
			                {value:uploadedFile, name: '已上传'},
			                {value:unuploadedFile, name: '未上传'}
			              ]
		        }
		   ]
		};
	//载入图表  
	loadDrugs(barChart,option1); 
	//载入图表  
	loadDrugs(barChart2,option2);
	
	barChart.setOption(option1);
	barChart.on("click", pieConsole1);
	barChart2.setOption(option2);
	barChart2.on("click", pieConsole2);
}

function pieConsole1(param) {
	var reportFrequency = $("div[id='titleSelector']").find("span.font-color-white").attr("reportFrequency");
	var isEstimateData = "";
	if(param.name == "未上传"){
		isEstimateData = "0";
	}
	if(param.name == "已上传(含无需上传)"){
		isEstimateData = "1";
	}
	//var url = "/solution/business/estimateInfo/estimateInfoListView.do?esstimateName="+param.name+"&esstimateType=ESTIMATE_DATA";
	var url = EST_PATH_PREFIX + "/estimateInfoListView.do?isEstimate="+isEstimateData+"&&reportFrequency="+reportFrequency+"&&type=ESTIMATE_DATA";
	window.location.href= url;
	/*var url = EST_PATH_PREFIX + "/estimateInfoListView.do?esstimateName="+param.name+"&esstimateType=ESTIMATE_DATA";
	openDialogNew(url);
	openDialogNew(url);*/
	
}
function pieConsole2(param) {
	var reportFrequency = $("div[id='titleSelector']").find("span.font-color-white").attr("reportFrequency");
	var isEstimateData = "";
	if(param.name == "未上传"){
		isEstimateData = "0";
	}
	if(param.name == "已上传"){
		isEstimateData = "1";
	}
	var url = EST_PATH_PREFIX + "/estimateInfoListView.do?isEstimate="+isEstimateData+"&&reportFrequency="+reportFrequency+"&&type=ESTIMATE_FILE";
	window.location.href= url;
	
	/*var url = EST_PATH_PREFIX + "/estimateInfoListView.do?esstimateName="+param.name+"&esstimateType=ESTIMATE_FILE";
	openDialogNew(url);*/
}

//加载
function loadDrugs(element,option) {  
	element.setOption(option, true);  
	element.hideLoading();
	window.onresize = function () {
		element.resize();
    };
} 
function getDateRange(param){
	  var dateRange = {};
	  var dateUtils = new DateUtils();
	  var startDate = null;
	  var endDate = null;
	  //根据选项指定时间段
	  switch (Number(param)) {
	  case 1:
	     //当日
	     startDate = dateUtils.getCurrentDate();
	     endDate = dateUtils.getCurrentDate();
	     break;
	  case 2:
	     //本周
	     startDate = dateUtils.getWeekStartDate();
	     endDate = dateUtils.getWeekEndDate();
	     break;
	  case 3:
	     //本月
	     startDate = dateUtils.getMonthStartDate();
	     endDate = dateUtils.getMonthEndDate();
	     break;
	  case 4:
	     //近三个月
	     startDate = dateUtils.getCurrentDate();
	     endDate = dateUtils.getThreeMonthsStartDate();
	     break;
	  case 5:
	     //近半年
	     startDate = dateUtils.getCurrentDate();
	     endDate = dateUtils.getHalfYearStartDate();
	     break;
	  case 6:
	     //近一年.
	     startDate = dateUtils.getCurrentDate();
	     endDate = dateUtils.getOneYearStartDate();
	     break;
	  default:
	     //当日
	     startDate = dateUtils.getCurrentDate();
	     endDate = dateUtils.getCurrentDate();
	     break;
	  }
	  dateRange["startDate"] = startDate;
	  dateRange["endDate"] = endDate;
	  return dateRange;
	}

function estimateBatchDownload(){
	var reportFrequency = $("div[id='titleSelector']").find("span.font-color-white").attr("reportFrequency");
	var message = "";
	if("1" == reportFrequency){
		message = "当日";
	}else if("2" == reportFrequency){
		message = "本周";
	}else if("3" == reportFrequency){
		message = "本月";
	}
	var b = getDateRange(reportFrequency);
	var startDate = b.startDate;
	var endDate = b.endDate;
	$.ajax({
		type:"POST",
		url: "/solution/business/estimateInfo/checkAttach.do",
		data:{"startDate" : startDate,
			  "endDate" : endDate
			},
		cache:false,
		dataType:"json",
		success:function(data){
			var a = data;
			if(data.Status == "success"){
				//var url = "/solution/business/estimateInfo/batchDownAttach2.do?startDate="+startDate+"&endDate="+endDate;
				var bizId = data.Data;
				var url = "/solution/business/myHome/todoAndOutput/batchEndFileDownload4Biz.do?bizType=ESTIMATE&bizId="+bizId;
				window.location.href= url;
			}else {
				ctools.alert(message+'没有可下载的估值文件!',"","warning");
			}
		}
	});
}